package com.example.yubo.broadnotifytest_403yb;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

/**
 * Created by yubo on 2017/5/4.
 */

public class MyReceiver extends BroadcastReceiver {
    private Context context;
    private String str = "";//接收到广播的数据
    //设置全局的  ma要用 清楚消息
    public static NotificationManager manager;
    public static int notifyID = 1;//通知的ID 用于清楚通知

    @Override
    public void onReceive(Context context, Intent intent) {
        //接受消息
        this.context = context;
        //一定和MainActivity 中明知对 para2 一样
        str = intent.getStringExtra("para2");
        Toast.makeText(context, "接收到广播：" + str, Toast.LENGTH_SHORT).show();
        //我们能接收到广播 我们已经在mf中注册了
        //安卓4个组件 actity recevier.....

        //收到广播之后    在这发送通知


//发送通知

        sendnotify();
    }

    private void sendnotify() {
        //1获取manager
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


        //2实例化builder
        //定义
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);


        //3配置builder****************
        //点击通知后打开新窗口
        Intent intent = new Intent(context, NotifyActivity.class);
        intent.putExtra("para3", str);
        // PendingIntent  有延迟的  不是立刻执行
        // 只有点击notifyID才进入新窗口
        PendingIntent pintent = PendingIntent.getActivity(context, notifyID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 配置一些默认必选的属性  最后才发送通知
        builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知标题").setContentText("通知内容" + str).setContentIntent(pintent)
                .setTicker("通知来了").setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true).setWhen(System.currentTimeMillis());


        //4发送通知
        //调用刚才的manageer
        manager.notify(notifyID, builder.build());
        //现在已经收到通知了


    }
}
